layui.use(['table', 'admin', 'ax','simTool','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var simTool = layui.simTool;
    var laydate = layui.laydate;
    /**
     * 优惠券管理
     */
    var Discount = {
        tableId: "discountTable"
    };

    var sim = new simTool();
    sim.updateButton("/discount/disableItem",Discount.tableId,"recordStatus",[1,0])

    /**
     * 初始化表格的列
     */
    Discount.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'id'},
            {field: 'type', sort: true, title: '类型',templet:"#typeTpl"},
            {field: 'name', sort: true, title: '名称'},
            {field: 'description', sort: true, title: '描述'},
            {field: 'amountTotal', sort: true, title: '将发放总数'},
            {field: 'amountOut', sort: true, title: '已发放总数'},
            {field: 'amountPer', sort: true, title: '每人限领'},
            {field: 'termDay', sort: true, title: '使用期限(天数)'},
            {field: 'leastPrice', sort: true, title: '满减金额',
                templet:function (d) {
                    return Feng.formatCurrency(d.leastPrice);
                }
            },
            {field: 'discountPrice', sort: true, title: '优惠金额',
                templet:function (d) {
                    return Feng.formatCurrency(d.discountPrice);
                }
            },
            {field: 'effectiveTime', sort: true, title: '生效时间'},
            {field: 'expireTime', sort: true, title: '过期时间'},
            {field: 'createTime', sort: true, title: '创建时间'},
            {field: 'recordStatus', sort: true, title: '状态',
                templet:"#statusTpl"
            },
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };
    /**dis
     * 点击查询按钮
     */
    Discount.search = function () {
        var queryData = {};
        queryData['name'] = $("#name").val();
        queryData['recordStatus'] = $("#recordStatus").val();

        if($("#effectiveTime").val()!=""){
            queryData['effectiveTime'] = $("#effectiveTime").val();
        }
        queryData['leastPriceMin'] = $("#leastPriceMin").val();
        queryData['leastPriceMax'] = $("#leastPriceMax").val();
        queryData['discountPriceMin'] = $("#discountPriceMin").val();
        queryData['discountPriceMax'] = $("#discountPriceMax").val();

        table.reload(Discount.tableId, {where: queryData,
            page:{curr:1}
        });
    };

    /**
     * 弹出添加对话框
     */
    Discount.openAddDlg = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加优惠券',
            maxmin:true,
            area:["50%","60%"],
            content: Feng.ctxPath + '/discount/add',
            end: function () {
                admin.getTempData('formOk') && table.reload(Discount.tableId);
            }
        });
    };

    /**
     * 导出excel按钮
     */
    Discount.exportExcel = function () {
        var checkRows = table.checkStatus(Discount.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    Discount.openEditDlg = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '修改优惠券',
            maxmin:true,
            area:["50%","60%"],
            content: Feng.ctxPath + '/discount/edit?id=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(Discount.tableId);
            }
        });
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Discount.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/discount/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Discount.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Discount.tableId,
        url: Feng.ctxPath + '/discount/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Discount.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Discount.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Discount.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Discount.exportExcel();
    });
    laydate.render({
        elem: '#effectiveTime',
        type: 'datetime'
    });
    // 工具条点击事件
    table.on('tool(' + Discount.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Discount.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Discount.onDeleteItem(data);
        }
    });
});
